import { takeEvery } from "redux-saga/effects";
import { NATIVE_CLASS } from "@common/constants";
import { callNativeFunc } from "@common/saga-helper";
import {
  SAGA_MOVE_DIRECTLY_PUSH_2_SERVER,
  SAGA_MOVE_DIRECTLY_DEL_DETAILS,
  SAGA_MOVE_DIRECTLY_FINISH_OPS,
  SAGA_MOVE_DIRECTLY_LOAD_DETAILS,
  SAGA_MOVE_DIRECTLY_LOAD_OPS_FORM,
  SAGA_MOVE_DIRECTLY_STASH_OPS_DATA,
} from "./saga-types";
import {
  DEL_MOVE_DIRECTLY_DETAILS,
  SET_MOVE_DIRECTLY_DATA_ID,
  SET_MOVE_DIRECTLY_DETAILS,
  SET_MOVE_DIRECTLY_OPS_FORM,
} from "./action-types";

function* stashMoveDirectlyOpsDataSaga() {
  yield takeEvery(SAGA_MOVE_DIRECTLY_STASH_OPS_DATA, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.MoveDirectlyOpsHandler,
            action: "create",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_MOVE_DIRECTLY_DATA_ID],
          callback: action.payload.callback,
        },
      ],
    });
  });
}

function* loadMoveDirectlyOpsFormDataSaga() {
  yield takeEvery(SAGA_MOVE_DIRECTLY_LOAD_OPS_FORM, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.MoveDirectlyOpsHandler,
            action: "loadOpsFormData",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_MOVE_DIRECTLY_OPS_FORM],
        },
      ],
    });
  });
}

function* loadMoveDirectlyDetailsSaga() {
  yield takeEvery(SAGA_MOVE_DIRECTLY_LOAD_DETAILS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.MoveDirectlyDetailsHandler,
            action: "loadDetails",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_MOVE_DIRECTLY_DETAILS],
        },
      ],
    });
  });
}

function* delMoveDirectlyDetailsSaga() {
  yield takeEvery(SAGA_MOVE_DIRECTLY_DEL_DETAILS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.MoveDirectlyDetailsHandler,
            action: "delete",
            needNonce: false,
            params: action.payload,
          },
          respActions: [DEL_MOVE_DIRECTLY_DETAILS],
          callback: action.payload.callback
        },
      ],
    });
  });
}

function* finishMoveDirectlyOpsSaga() {
  yield takeEvery(SAGA_MOVE_DIRECTLY_FINISH_OPS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.MoveDirectlyOpsHandler,
            action: "finish",
            needNonce: false,
            params: action.payload,
          },
          callback: action.payload.callback,
        },
      ],
    });
  });
}

function* pushMoveDirectly2ServerSaga() {
  yield takeEvery(SAGA_MOVE_DIRECTLY_PUSH_2_SERVER, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.MoveDirectlyHandler,
            action: "push2Server",
            needNonce: true,
            params: action.payload,
          },
          callback: action.payload.callback
        },
      ],
    });
  });
}

export default [
  stashMoveDirectlyOpsDataSaga(),
  loadMoveDirectlyOpsFormDataSaga(),
  loadMoveDirectlyDetailsSaga(),
  delMoveDirectlyDetailsSaga(),
  finishMoveDirectlyOpsSaga(),
  pushMoveDirectly2ServerSaga()
];
